package com.zmn.oms.admin.controller.price;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.business.interfaces.price.PriceTableBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.dto.product.ProductQueryDTO;
import com.zmn.oms.model.entity.factory.OrderFactory;
import com.zmn.oms.model.entity.product.OrderProduct;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.price_list.FaultPriceListVO;
import com.zmn.oms.model.vo.price_list.PriceListVO;
import com.zmn.oms.services.interfaces.factory.OrderFactoryService;
import com.zmn.oms.services.interfaces.product.OrderProductService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 1. 产品根据城市定价
 * 2. 使用三级结构：服务类型、二级分类、产品
 *
 * @author
 * @类说明：产品价格表
 */
@Controller
@RequestMapping(value = "/work/price")
public class ProductPriceTableController extends ProductPriceBaseController {

    @Autowired
    PriceTableBService priceTableBService;
    @Autowired
    OrderWorkService orderWorkService;
    @Autowired
    OrderProductService orderProductService;
    @Autowired
    OrderFactoryService orderFactoryService;

    /**
     * 价格表服务类型和城市
     *
     * @author
     * @类说明：产品价格表
     */
    @RequestMapping(value = "/table/list")
    public ModelAndView getPriceTable() {
        ModelAndView mv = new ModelAndView("price/table/list");
        this.processMV(mv);
        return mv;
    }

    /**
     * 根据产品Id和城市Id 获取对应价格表明细
     *
     * @author
     * @类说明：产品价格表
     */
    @RequestMapping(value = "/table/detail", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO getTableDetail(Integer productId,
                                    Integer cityId,
                                    Integer channelId,
                                    Integer brandId,
                                    Integer bizType,
                                    Integer productShowType,
                                    @RequestParam(required = false) Integer type,
                                    @RequestParam(required = false) Integer plat,
                                    @RequestParam(required = false) Long workId,
                                    @RequestParam(required = false) Long orderId) throws OmsBaseException {
        PriceListVO priceList = priceTableBService.getProductPriceTable(productId, cityId, channelId, bizType, brandId, productShowType, orderId, workId, type, plat, null, null);

        Map<String, PriceListVO> priceListMap = new HashMap<>();
        priceListMap.put("priceListVO", priceList);

        return ResultDTO.success(priceListMap);
    }

    /**
     * 价格查询（详情页）
     */
    @RequestMapping(value = "/order/detail")
    public ModelAndView detailPriceList(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("price/order/list");

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        List<OrderProduct> orderProductList = orderProductService.listOrderProductByOrderId(orderId);
        Integer brandId = null;
        boolean brandIdBoolean = CollectionUtils.isNotEmpty(orderProductList) &&
                Objects.nonNull(orderProductList.get(0).getBrandId()) &&
                !Objects.equals(orderProductList.get(0).getBrandId(), 0);
        if (brandIdBoolean) {
            brandId = orderProductList.get(0).getBrandId();
        } else if (Objects.equals(GlobalConsts.PLAT_MARK_YEYX, orderWork.getPlat())) {
            OrderFactory orderFactoryByKey = orderFactoryService.findOrderFactoryByKey(orderId);
            if (Objects.nonNull(orderFactoryByKey)) {
                brandId = orderFactoryByKey.getFcBrandId();
            }
        } else {
            brandId = 1051;
        }

        mv.addObject("orderWork", orderWork);
        mv.addObject("productShowType", 1);
        mv.addObject("brandId", brandId);
        ProductQueryDTO productQueryDTO = ProductQueryDTO.builder().cityId(orderWork.getCityId())
                .channelId(orderWork.getChannelId()).servCategId(orderWork.getServCategId()).categId(orderWork.getCategId()).showType(1).build();

        mv.addObject("productList", super.getServProductListByCategId(productQueryDTO));
        mv.addObject("brandList", super.getBrandList());

        return mv;
    }

    /**
     * 故障价格表服务类型和城市
     *
     * @author
     * @类说明：故障价格表
     */
    @RequestMapping(value = "/fault/list")
    public ModelAndView getFaultTable() {
        ModelAndView mv = new ModelAndView("price/fault/list");
        this.processMV(mv);
        return mv;
    }

    /**
     * 根据产品Id和城市Id 获取对应价格表明细
     *
     * @author
     * @类说明：故障价格表
     */
    @RequestMapping(value = "/fault/detail", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO getFaultDetail(Integer productId, Integer cityId, Integer channelId, Integer brandId) throws OmsBaseException {

        FaultPriceListVO priceListIn = priceTableBService.getFaultPriceTable(productId, cityId, channelId, com.zmn.consts.GlobalConsts.BIZ_TYPE_F, brandId);
        FaultPriceListVO priceListOut = priceTableBService.getFaultPriceTable(productId, cityId, channelId, com.zmn.consts.GlobalConsts.BIZ_TYPE_C, brandId);

        Map<String, FaultPriceListVO> priceListMap = new HashMap<>();
        priceListMap.put("priceListIn", priceListIn);
        priceListMap.put("priceListOut", priceListOut);

        return ResultDTO.success(priceListMap);
    }

    private void processMV(ModelAndView mv) {

		// 城市集合 common
		List<KVDict> cityList = super.getCityListByDept();
		// 默认第一个选中
		if (CollectionUtil.isNotNullOrEmpty(cityList)) {
			cityList.get(0).setSelected(true);
		}
		mv.addObject("cityList", cityList);

		// 商家渠道 common
		List<KVDict> channelList = super.getAllChannelListByPermit();
		// 默认第一个选中
		if (CollectionUtil.isNotNullOrEmpty(channelList)) {
			channelList.get(0).setSelected(true);
		}
		mv.addObject("channelList", channelList);

        // 服务分类 common
        mv.addObject("servCategList", super.getShowServCategList());

        // 产品类型
        mv.addObject("productShowType", super.dictModel2KVDict(OrderConsts.getProductShowTypeList()));

        // 服务分类 common
        mv.addObject("servCategList", super.getShowServCategList());

        // 产品二级分类 common
        // mv.addObject("twoCategoryList", getTwoCategoryList());
        // 品牌ID
        mv.addObject("brandList", super.getBrandList());

        // 是否
        mv.addObject("yesOrNo", OrderConsts.getYesOrNoObj());
        mv.addObject("bizTypeList", OrderConsts.getOrderBizTypeList());
    }
}
